package com.cgx.tablet.beeper.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.adapter.FragmentsPagerAdapter;
import com.cgx.tablet.beeper.bean.CallBean;
import com.cgx.tablet.beeper.databinding.FragmentTicketNoTransferBinding;
import com.cgx.tablet.beeper.event.TicketTransEvent;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * A simple {@link Fragment} subclass.
 */
public class TicketNoTransferFragment extends DialogFragment {

    private Fragment[] fragments;

    private FragmentTicketNoTransferBinding mBinding;
    public static TicketNoTransferFragment newInstance(CallBean callBean){
        TicketNoTransferFragment ticketNoTransferFragment = new TicketNoTransferFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("callBean", callBean);
        ticketNoTransferFragment.setArguments(bundle);
        return ticketNoTransferFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setStyle(STYLE_NORMAL, R.style.Dialog_FullScreen);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //设置布局
        View view = inflater.inflate(R.layout.fragment_ticket_no_transfer, container, false);
        ScreenAdapterTools.getInstance().loadView(view);
        mBinding = DataBindingUtil.bind(view);
        mBinding.closeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onDestroy();
            }
        });
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        Window window = getDialog().getWindow();
//        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        initViewPager();
    }

        private void initViewPager() {
        initFragments();
        String[] mTitles = new String[]{getResources().getString(R.string.biz_trans), getResources().getString(R.string.win_trans)};
        FragmentsPagerAdapter fragmentPagerAdapter = new FragmentsPagerAdapter(getChildFragmentManager(), mTitles, fragments);
        mBinding.ticketNoTransferVp.setAdapter(fragmentPagerAdapter);
        mBinding.ticketNoTransferTabLayout.setupWithViewPager(mBinding.ticketNoTransferVp);
        mBinding.ticketNoTransferVp.setOffscreenPageLimit(2);
        mBinding.ticketNoTransferVp.setCurrentItem(0, false);
    }

    private void initFragments() {
        //业务转移
        BizTransFragment bizTransFragment = BizTransFragment.newInstance((CallBean) getArguments().getParcelable("callBean"));
        //窗口转移
        WinTransFragment winTransFragment = WinTransFragment.newInstance((CallBean) getArguments().getParcelable("callBean"));
        fragments = new Fragment[]{bizTransFragment, winTransFragment};
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(TicketTransEvent ticketTransEvent) {
        ToastUtil.showShort(getActivity(), R.string.trans_success);
        dismiss();
        onDestroy();
    }
}
